#include <iostream>
#include <algorithm>
#include <vector>

using std::cout;
using std::endl;
using std::find;
using std::vector;

int test0()
{
    vector<int> numbers{1, 2, 3, 4, 5};
    //时间复杂度O(N)
    auto it = find(numbers.begin(), numbers.end(), 3);
    if(it != numbers.end()) {
        cout << "查找成功" << endl;
        cout << "*it:" << *it << endl;
    } else {
        cout << "查找失败, not found" << endl;
    }

    return 0;
}

void test1()
{
    cout << "test1() " << endl;
    vector<int> numbers{3, 4, 5, 2, 7, 8, 1, 6, 9, 10};
    //std::sort要求是随机访问迭代器
    std::sort(numbers.begin(), numbers.end());
    //再次进行查找操作, 采用了二分查找, 可以提高效率
    bool ret = std::binary_search(numbers.begin(), numbers.end(), 5);
    if(ret) {
        cout << "查找成功, 5 在容器中" << endl;
    } else {
        cout << "查找失败, 5 不在容器中" << endl;
    }
}

int main()
{
    /* test0(); */
    test1();
}

